package main

import (
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/lib/pq"
)

type s struct {
	Sno   int    `gorm:"not null"`
	Sname string `gorm:"not null"`
}

func main() {
	db, err := gorm.Open("postgres", "host=127.0.0.1 user=postgres dbname=test password=Tss1433223 sslmode=disable")
	if err != nil {
		panic(err)
	}
	fmt.Println("连接成功！")

	/* 	if !db.HasTable(&s{}) {
	   		if err := db.CreateTable(&s{}).Error; err != nil {
	   			panic(err)
	   		} else {
	   			fmt.Println("创建成功！")
	   		}
	   	}

	   	u1 := s{1, "Li"}
	   	if err := db.Create(&u1).Error; err != nil {
	   		fmt.Println("插入失败！", err)
	   		return
	   	} else {
	   		fmt.Println("添加成功！")
	   	} */

	var u s
	db.Find(&u, "Sno=?", 1)
	fmt.Printf("查询成功：")
	fmt.Println(u)

	defer db.Close()
}
